package com.lcl.jarvis.filter;

import com.lcl.jarvis.util.JwtUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class JwtTokenFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

//        String token = request.getHeader("Authorization"); // 获取token
//        if (token != null && token.startsWith("Bearer ")) {
//            String jwt = token.substring(7); // 去除"Bearer "前缀
//
//            // 验证JWT token
//            String username = JwtUtils.verifyAndDecodeJWT(jwt);
//            System.out.println("username: " + username);
//
//            if (username != null) {
//                // 验证通过，继续处理请求
//                filterChain.doFilter(request, response);
//            } else {
//                // 验证不通过，返回未授权错误
//                response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
//            }
//        } else {
//            // 没有提供token，返回未授权错误
//            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
//        }

        // 验证通过，继续处理请求
        filterChain.doFilter(request, response);

    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
